<?php
    defined( "AZ_EXE_" ) or die ( "CORE VARIABLE IS NOT DEFINED." );
    class UserExpireController extends Controller {
        public function view(){
            $id = Basic::getVar("id");
            $article = $this->loadModel("Article");
            if ( $id ){
                $this->set("selectedMenu","ART");
                $this->setLanguage( "English" );
                $this->set("title","SMECA - Article View");
                $articleinfo = $article->find( $id );
                $this->set("article",$articleinfo[0]);
                $this->renderDefault("view", "article" );
            }else{
                $this->index();
            }
        }
        
        public function index(){
            $start = Basic::getVar("start");
            $perpage = Basic::getVar("perpage");
            $search = Basic::getVar("search");
            $start = is_numeric($start)&&$start>-1?$start:0;
            $perpage = is_numeric($perpage)&&$perpage>-1?$perpage:20;
            $searchColumns = Basic::getVar("search_column")?Basic::getVar("search_column"):false;
            $sortColumn = Basic::getVar("sort_column")?Basic::getVar("sort_column"):"ue.id,ue.created,ue.modified";
            $sortCriteria = Basic::getVar("sort_criteria")?Basic::getVar("sort_criteria"):false;
            $expire = $this->loadModel("Expire");
            $expireRecords = $expire->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
            $totalRecords = $expire->getTotalRecords();
            if ( $start < $totalRecords ){
                $this->set("totalRows",$totalRecords);
            }elseif ( $start >= $totalRecords ) {
                $start = $start-20;
                $expireRecords = $expire->display($start,$perpage,$search,$searchColumns,$sortColumn,$sortCriteria) ;
                $this->set("totalRows",$totalRecords);
            }
            $this->set("currentPage",$start);
            $this->set("perpage",$perpage);
            $this->set("search",$search);
            $this->set("searchColumns",$searchColumns);
            $this->set("sortColumn",$sortColumn);
            $this->set("sortCriteria",$sortCriteria);
            $this->set("search",$search);
            $this->set("expires",$expireRecords);
            $this->set("selectedMenu","USR EXP");
            
            $this->set( "title" , "SMECA - User Expire Date List" );
            $this->setLanguage( "English" );
            $this->renderDefault("display","expire");
        }   
        public function add(){
            $user = $this->loadModel("User");
            $expire = $this->loadModel("Expire");
            $expire_infos = $expire->getExpires();
            $user_infos = $user->premiumUser();
            
            $expire_datas = array(); 
            foreach( $expire_infos As $key => $expire_info ){
                $expire_datas[$expire_info->id] = $expire_info->label . " : " . $expire_info->price . " $";
            }
            $this->set('expires',$expire_datas);
            
            $user_datas = array(); 
            foreach( $user_infos As $key => $user_data ){
                $user_datas[$user_data->id] = $user_data->first_name . " " . $user_data->last_name ;
            }
            $this->set('users',$user_datas);
            
            $this->set("selectedMenu","USR EXP");
            $this->set( "title" , "SMECA - User Expiry Date New" );
            $this->setLanguage( "English" );
            $this->renderDefault("adduserexpire","expire" );
        }
        public function edit(){
            $id = Basic::getVar("id");
            
            $user = $this->loadModel("User");
            $expire = $this->loadModel("Expire");
            $user_infos = $user->premiumUser();
            $expire_infos = $expire->getExpires();
            
            $expire_datas = array(); 
            foreach( $expire_infos As $key => $expire_info ){
                $expire_datas[$expire_info->id] = $expire_info->label . " : " . $expire_info->price . " $";
            }
            $this->set('expires',$expire_datas);
            
            $user_datas = array(); 
            foreach( $user_infos As $key => $user_data ){
                $user_datas[$user_data->id] = $user_data->first_name . " " . $user_data->last_name ;
            }
            $this->set('users',$user_datas);
            
            $this->set("selectedMenu","USR EXP");
            $this->set( "title" , "SMECA - User Expiry Date Edit" );
            $this->setLanguage( "English" );
            if ( $id ){
                $userExpire = $this->loadModel("Expire");
                $expire_info = $userExpire->find( $id );
                $this->set("expire_info",$expire_info[0]);
                $this->renderDefault("edit", "expire" );
            }else{
                $this->index();
            }
        }
        public function editprocess(){
            $expire = $this->loadModel("Expire");
            $user = SESSION::get("user");
            $_POST['data']['userexpireds']['modified_by'] = $user->id;
            if( $expire->edit($_POST['data']) ){
                $this->set("info","User expiry date information is updated succeffully.");
            }else{
                $this->set("info","Failed to update User expiry date information.");
            }
            $this->redirect( array("com"=>"expire","action"=>"index"));
        } 
        public function addprocess(){
            $article = $this->loadModel("Expire");
            $user = SESSION::get("user");
            $_POST['data']['userexpireds ']['modified_by'] = $user->id;
            if( $article->add($_POST['data']) ){
                $this->set("info","User expiry date is added successfully.");
            }
            else{
                $this->set("info","Failed to add user expiry date.");
            } 
            $this->redirect( array("com"=>"expire","action"=>"index"));
        }
        public function delete(){
            $id = Basic::getVar("id");
            $user = $this->loadModel("Expire");
            if ( $id ){
                $this->set("info","User expiry date(s) delete successfully.");
                $user->delete( $id );
            }else{
                $this->set("info","Failed to delete user expiry date(s).");
            }
            $this->redirect( array("com"=>"expire","action"=>"index"));
        }
    }
?>